package com.sctvcloud.bazhong.ui.utils;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.ijkplaylib.utils.PlayerUtils;
import com.ruihang.ijkplaylib.widget.GPlayerView;
import com.ruihang.ijkplaylib.widget.IjkVideoView;
import com.sctvcloud.bazhong.R;

/**
 * Created by liuyu on 2017/8/3.
 */

public class GPlayerBottomControllerImp extends AbsLivingBottomController {

    private CustomFontTextView playedTime, totalTime;
    private SeekBar seekBar;
    private boolean isDragging;

    private boolean hasBarrage;

    /**
     * 进度条滑动监听
     */
    private final SeekBar.OnSeekBarChangeListener mSeekListener = new SeekBar.OnSeekBarChangeListener() {

        /**数值的改变*/
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if (!fromUser) {
                /**不是用户拖动的，自动播放滑动的情况*/
                return;
            } else {
                GPlayerView playerView = getPlayerTemp();
                if (playerView == null) {
                    return;
                }
                long duration = playerView.getDuration();
                int position = (int) ((duration * progress * 1.0) / 1000);
                String time = PlayerUtils.generateTime(position);
                playedTime.setText(time);
//                query.id(ResourceUtils.getResourceIdByName(mContext, "id", "app_video_currentTime")).text(time);
//                query.id(ResourceUtils.getResourceIdByName(mContext, "id", "app_video_currentTime_full")).text(time);
//                query.id(ResourceUtils.getResourceIdByName(mContext, "id", "app_video_currentTime_left")).text(time);
            }

        }

        /**开始拖动*/
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            isDragging = true;
            GPlayerView playerView = getPlayerTemp();
            if (playerView != null) {
                playerView.removeMsg(GPlayerView.MESSAGE_SHOW_PROGRESS);
            }
        }

        /**停止拖动*/
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            GPlayerView playerView = getPlayerTemp();
            if (playerView == null || playerView.getVideoView() == null) {
                return;
            }
            long duration = playerView.getDuration();
            playerView.getVideoView().seekTo((int) ((duration * seekBar.getProgress() * 1.0) / 1000));
            playerView.removeMsg(GPlayerView.MESSAGE_SHOW_PROGRESS);
            isDragging = false;
            playerView.sendEmptyMsg(GPlayerView.MESSAGE_SHOW_PROGRESS, 1000);
        }
    };

    public GPlayerBottomControllerImp(AppCompatActivity activity) {
        super(activity);
    }

    @NonNull
    @Override
    protected View onCreateView(GPlayerView playerView, LayoutInflater inflater, ViewGroup group) {
        View v = inflater.inflate(R.layout.gplay_bottom_controll, group, false);
        playedTime = (CustomFontTextView) v.findViewById(R.id.gplayer_bottom_time_played);
        totalTime = (CustomFontTextView) v.findViewById(R.id.gplayer_bottom_time_end);
        seekBar = (SeekBar) v.findViewById(R.id.gplayer_bottom_seek);
        seekBar.setMax(1000);
        seekBar.setOnSeekBarChangeListener(mSeekListener);
        playBtn = (ImageView) v.findViewById(R.id.living_top_img_play);
        barrageBtn = (CustomFontTextView) v.findViewById(R.id.living_top_barrage);
        fullBtn = (CustomFontTextView) v.findViewById(R.id.living_top_full_screen);
        return v;
    }

    @Override
    public long syncProgress(IjkVideoView videoView) {
        if (videoView == null || isDragging) {
            return 0L;
        }
        long position = videoView.getCurrentPosition();
        long duration = videoView.getDuration();
        if (seekBar != null) {
            if (duration > 0) {
                long pos = 1000L * position / duration;
                seekBar.setProgress((int) pos);
            }
            int percent = videoView.getBufferPercentage();
            seekBar.setSecondaryProgress(percent * 10);
        }
        if (playedTime != null) {
            playedTime.setText(PlayerUtils.generateTime(position));
        }
        if (totalTime != null) {
            totalTime.setText(PlayerUtils.generateTime(duration));
        }

//        query.id(ResourceUtils.getResourceIdByName(mContext, "id", "app_video_currentTime")).text(generateTime(position));
//        query.id(ResourceUtils.getResourceIdByName(mContext, "id", "app_video_currentTime_full")).text(generateTime(position));
//        query.id(ResourceUtils.getResourceIdByName(mContext, "id", "app_video_currentTime_left")).text(generateTime(position));
//        query.id(ResourceUtils.getResourceIdByName(mContext, "id", "app_video_endTime")).text(generateTime(duration));
//        query.id(ResourceUtils.getResourceIdByName(mContext, "id", "app_video_endTime_full")).text(generateTime(duration));
//        query.id(ResourceUtils.getResourceIdByName(mContext, "id", "app_video_endTime_left")).text(generateTime(duration));
        return position;
    }

    @Override
    public void setHasBarrage(boolean hasBarrage) {
        this.hasBarrage = hasBarrage;
    }

    @Override
    public void onPortrait() {
        if (barrageBtn != null) {
            barrageBtn.setVisibility(View.GONE);
        }
    }

    @Override
    public void onLandscape() {
        if (barrageBtn != null && hasBarrage) {
            barrageBtn.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (seekBar != null) {
            seekBar.setOnSeekBarChangeListener(null);
        }
    }
}
